package com.rongcheng.fortestlist.EXELFrameworkDemo.fragment.MultiTypeQuestion;

import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.rongcheng.fortestlist.EXELFrameworkDemo.fragment.QABaseFragment;
import com.rongcheng.fortestlist.R;

import static com.rongcheng.fortestlist.R.id.viewPager;

/**
 * 复合题
 *
 * @author frc
 *         created at 17-1-4.
 */

public abstract class BaseMultiQuestionFragment extends QABaseFragment {
    ImageView iv_splitter;
    FrameLayout top_frameLayout;
    ViewPager viewPager;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_multi_question, container, false);
        viewPager = (ViewPager) view.findViewById(R.id.view_pager);
        iv_splitter = (ImageView) view.findViewById(R.id.iv_splitter);
        top_frameLayout = (FrameLayout) view.findViewById(R.id.top_fragment);

        FragmentManager fm = getChildFragmentManager();
        Fragment topFragment = fm.findFragmentById(R.id.top_fragment);
        if (topFragment == null) {
            topFragment = getTopFragment();
            fm.beginTransaction().add(R.id.top_fragment, topFragment).commit();
        }

        viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return QABaseFragment.getFragment(node.mChildren.get(position));
            }

            @Override
            public int getCount() {
                return node.mChildren.size();
            }

            @Override
            public Parcelable saveState() {
                return null;
            }
        });
        iv_splitter.setOnTouchListener(onTouchListener);
        return view;


    }

    View.OnTouchListener onTouchListener = new View.OnTouchListener() {
        private float startY;
        private float startTop;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            LinearLayout parentView = (LinearLayout) v.getParent();

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    startTop = v.getTop();
                    startY = event.getRawY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    float top = startTop + (event.getRawY() - startY);
                    top = Math.max(0, Math.min(top, parentView.getHeight() - v.getHeight()));
                    ViewGroup.LayoutParams params = v.getLayoutParams();
                    float topHeight = top;
                    float bottomHeight = parentView.getHeight() - topHeight - v.getHeight();

                    LinearLayout.LayoutParams topParams = (LinearLayout.LayoutParams) top_frameLayout.getLayoutParams();
                    LinearLayout.LayoutParams bottomParams = (LinearLayout.LayoutParams) viewPager.getLayoutParams();
                    topParams.weight = topHeight;
                    bottomParams.weight = bottomHeight;
                    top_frameLayout.setLayoutParams(topParams);
                    viewPager.setLayoutParams(bottomParams);
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_CANCEL:
                    break;
            }
            return true;
        }
    };

    protected abstract Fragment getTopFragment();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}
